import axios from 'axios'
import store from '../store/index'
import router from '../router'
// import vue from '../main'
import { baseUrl } from './url.js'
import { getRequest } from './tools'
const qs = require('qs')
// Authorization  Basic c3dvcmQ6c3dvcmRfc2VjcmV0
export let instance = axios.create({
  timeout: 100000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencodedcharset=UTF-8',
    Authorization: 'Basic c3dvcmQ6c3dvcmRfc2VjcmV0',
    'X-Requested-With': 'XMLHttpRequest'
  }
})
if (process.env.NODE_ENV === 'production') {
  instance.defaults.baseURL = baseUrl
}

let urlArr = []
let flagObj = {}
instance.interceptors.request.use(
  (config) => {
    let token = window.localStorage.getItem('accessToken')
    if (token) config.headers.common['blade-auth'] = 'bearer ' + token
    let freshToken = getRequest('refreshToken', '?' + config.data)
    if (urlArr.includes(config.url)) {
      config.headers['Content-Type'] =
        'application/x-www-form-urlencodedcharset=UTF-8'
      if (freshToken) {
        store.commit('controlLoading', false)
        !config.closeLoading && store.commit('controlLoading', true)
      }
    } else {
      config.headers['Content-Type'] = 'application/json'

      if (!config.closeLoading) {
        store.commit('controlLoading', true)
        flagObj[config.url] = true
      }
    }
    return config
  },
  (err) => {
    store.commit('controlLoading', false)
    return Promise.reject(err)
  }
)

instance.interceptors.response.use(
  (response) => {
    flagObj[response.config.url] = false
    store.commit('controlLoading', Object.values(flagObj).includes(true))
    if (response.status === 200) {
      return Promise.resolve(response.data)
    } else {
      return Promise.reject(response.data)
    }
  },
  (error) => {
    let { status,
      // data,
      config } = error.response || {}
    flagObj[config.url] = false
    !config.closeLoading &&
      store.commit('controlLoading', Object.values(flagObj).includes(true))
    // let { msg } = data
    // let token = window.localStorage.getItem('accessToken')
    store.commit('controlLoading', false)
    if (status) {
      switch (status) {
        case 400: {
          // console.log('dddddddddddddddddddddd',data)
          // vue.$message.error(data.msg)
          break
        }
        case 401: {
          // if (token) vue.$message.error(msg)
          // // 清除登录人名称
          window.localStorage.removeItem('accessToken')
          window.localStorage.removeItem('userName')
          window.localStorage.removeItem('userId')
          router.push('/login')
          break
        }
        case 403:
          break
        case 404:
          break
        case 500:
          // vue.$message.error(msg)
          break
        default:
      }
      return Promise.reject(error.response)
    }
  }
)

export const get = (url, params) => instance.get(url, { params, closeLoading: true })
export const getForm = (url, params) =>
  instance.get(
    url,
    { params, closeLoading: true },
    {
      headers: { 'content-type': 'application/x-www-form-urlencoded' }
    }
  )
export const post = (url, data) => {
  return instance.post(url, data, { closeLoading: true })
}
export const postForm = (url, data) =>
  instance.post(url, qs.stringify(data), {
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    closeLoading: true
  })
